package com.dream.service.impl;

import com.dream.dao.UserDao;
import com.dream.domain.user.User;
import com.dream.dto.user.registration.RegistrationDTO;
import com.dream.service.UserService;
import org.springframework.security.authentication.encoding.PasswordEncoder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

/**
 * Created by IntelliJ IDEA.
 * User: Gang Zhong
 * Date: 13-3-15
 * Time: 上午1:31
 */
public class UserServiceImpl implements UserService {
    private UserDao userDao;
    private PasswordEncoder passwordEncoder;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        return userDao.findUserByUsername(username);
    }

    @Override
    public void createUser(RegistrationDTO registrationDTO) {
        User user = registrationDTO.toNewUser(passwordEncoder);
        userDao.saveOrUpdate(user);
    }

    @Override
    public boolean emailExist(String email) {
        int count = userDao.findEmailCount(email);
        return count > 0;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public void setPasswordEncoder(PasswordEncoder passwordEncoder) {
        this.passwordEncoder = passwordEncoder;
    }
}
